<?php
namespace app\admin\controller;

use think\Controller;
use think\Db;
use app\admin\model\NodeModel;
use app\admin\model\WebsetModel;
use app\admin\model\UserModel;
use app\admin\model\MsgModel;
class Base extends Controller
{
    public function _initialize()
    {
        if(empty(session('username'))){

            $loginUrl = url('login/index');
            if(request()->isAjax()){
                return msg(111, $loginUrl, '登录超时');
            }

            $this->redirect($loginUrl);
        }

        // 检测权限
        $control = lcfirst(request()->controller());
        $action = lcfirst(request()->action());

        if(empty(authCheck($control . '/' . $action))){
            if(request()->isAjax()){
                return msg(403, '', '您没有权限');
            }

            $this->error('403 您没有权限');
        }

        $this->assign([
            'head'     => session('head'),
            'username' => session('username'),
            'rolename' => session('role'),
			'user_id'  => session('id')
        ]);

		$node = new NodeModel();
        $this->assign([
            'menu' => $node->getMenu(session('rule'))
        ]);

		$webset = new WebsetModel();
		$this->assign([
			'Webset'=>$webset->getInfo()
		]);
		// 记录操作日志
		if($this->request->isAjax()){
			$this->setLog();
		}
    }

	/**
	 * [记录操作日志]
	 */
	public function setLog(){
        $operationController = strtolower($this->request->controller());
        $operationAction = strtolower($this->request->action());
		if(!in_array($operationAction,config('logignore'))){
			$param = input();
			$param_res = [];
			foreach($param as $k => $v){
				if(is_array($v)){
					$param_res[] = $k . " => " . json_encode($v);
				}else{
					$param_res[] = $k . " => " . $v;
				}
			}
			unset($param_res[0]);
			$param_res = implode("{-}",$param_res);
	        $operationName = DB::name('node')->where([
	            'control_name'=>$operationController,
	            'action_name'=>$operationAction
	        ])->value('node_name');
	        if($operationName != ''){
				$logData = [
		            'c_time' => time(),
		            'url' => url(),
		            'url_name' => $operationName,
		            'u_id' => session('id'),
		            'user_name' => session('username'),
					'ip' => request()->ip(),
					'params' => $param_res
		        ];
		        DB::name('log')->insert($logData);
	        }
		}
	}
	/**
	 * [消息推送]
	 */
	public function socketSendMsg($uid='',$msg='',$send_id=0) {
		// 验证 判断发送人和接收人
		if($uid == ''){
			return ['code'=>0,'msg'=>'请先择收信人'];
		}
		if($send_id != 0){
			$userModel = new UserModel();
			$userInfo = $userModel->where('id',$send_id)->field('id,real_name,head')->find();
			if(empty($userInfo)){
				return ['code'=>0,'msg'=>'发送者不存在'];
			}
		}else{
			$websetModel = new WebsetModel();
			$webInfo = $websetModel->getInfo();
			$userInfo = [
				"id" => 0,
				"real_name" => $webInfo['title'],
				"head" =>$webInfo['mini_logo'],
			];
		}
		$savedMsg = socketMsg_encode($send_id,$userInfo,$msg);

		// 写入数据库
		$msgModel = new MsgModel();
		$msgModel->saveMsgPush($uid,$send_id,$savedMsg,$msg);
		// 推送并返回结果
		$userInfo['msg_name'] = "系统推送消息";
		$chatRoomInfo = $msgModel->where('msg_type',1)->find();
		$userInfo['chat_room_id'] = $chatRoomInfo['id'];
		$userInfo['chat_room_name'] = $chatRoomInfo['msg_name'];
		return send_socket_msg($uid,$msg,$userInfo);
	}
}
